﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace geometry
{
    public interface Shape
    {
        double Getarea();
        double Getlength();
        bool Islegal();
    }

    public class Rectangle : Shape
    {
        private double width;
        private double length;
        public double Width
        {
            get { return this.width; }
        }
        public double Length
        {
            get { return this.length; }
        }



        public Rectangle(double a,double b)
        {
            width = a; length = b;
            if (Islegal())
                WriteLine("成功构建一个长方形！");
            else WriteLine("长方形不合法！");
        }

        public double Getarea()
        {
            if (Islegal())
                return width * length;
            else return 0;
        }

        public double Getlength()
        {
            if (Islegal())
                return 2 * width + 2 * length;
            else return 0;
        }

        public bool Islegal()
        {
            if (width > 0 && length > 0) return true;
            else return false;
        }
    }

    public class Square : Shape
    {
        private double side;
        public double Side
        {
            get { return this.side; }
        }



        public Square(double a)
        {
            side = a;
            if (Islegal())
            {
                WriteLine("成功构建一个正方形！");
            }
            else WriteLine("正方形不合法！");
        }

        public double Getarea()
        {
            if (Islegal())
                return side * side;
            else return 0;
        }

        public double Getlength()
        {
            if (Islegal())
                return 4 * side;
            else return 0;
        }

        public bool Islegal()
        {
            if (side > 0 ) return true;
            else return false;
        }
    }

    public class Triangle : Shape
    {
        private double edge1;
        private double edge2;
        private double edge3;
        
        public double Edge1
        {
            get { return this.edge1; }
        }
        public double Edge2
        {
            get {return this.edge2; }
        }
        public double Edge3
        {
            get { return this.edge3; }
        }


        public Triangle(double a,double b,double c)
        {
            edge1 = a; edge2 = b; edge3 = c;
            if (Islegal())
            { 
                WriteLine("成功构建一个三角形！");
            }
            else WriteLine("三角形不合法！");
        }

        public double Getarea()
        {
            if (Islegal())
            {
                double p = (edge1 + edge2 + edge3) / 2;
                return Math.Sqrt(p * (p - edge1) * (p - edge2) * (p - edge3));
            }
            else return 0;
        }

        public double Getlength()
        {
            if (Islegal())
                return edge1 + edge2 + edge3;
            else return 0;
        }

        public bool Islegal()
        {
            if (edge1 > 0 && edge2 > 0 && edge3>0 && edge1+edge2>edge3 && edge1 + edge3 > edge2 && edge3 + edge2 > edge1)
                return true;
            else return false;
        }
    }

    public class ShapeFactory
    {
        public static Shape Getshape(int n)
        {
            Random ran = new Random();
            switch (n)
            {
                case 0:return new Rectangle(ran.Next(50),ran.Next(50));
                case 1:return new Square(ran.Next(50));
                case 2:return new Triangle(ran.Next(50), ran.Next(50), ran.Next(50));
                default:return null;
            }
        }
    }

    public class show
    {
        public static void dis() {
            string s;
            double a, b, c;
            double area = 0;
            double sum = 0;
            while (true)
            {
                Write("输入你想建立的图形：");
                s = ReadLine();
                if (s == "长方形")
                {
                    Write("输入长：");
                    a = Convert.ToDouble(ReadLine());
                    Write("输入宽：");
                    b = Convert.ToDouble(ReadLine());
                    Rectangle rec = new Rectangle(a, b);
                    area = rec.Getarea();
                }
                else if (s == "正方形")
                {
                    Write("输入边长：");
                    a = Convert.ToDouble(ReadLine());
                    Square sq = new Square(a);
                    area = sq.Getarea();
                }
                else if (s == "三角形")
                {
                    Write("输入第一边：");
                    a = Convert.ToDouble(ReadLine());
                    Write("输入第二边：");
                    b = Convert.ToDouble(ReadLine());
                    Write("输入第三边：");
                    c = Convert.ToDouble(ReadLine());
                    Triangle tri = new Triangle(a, b, c);
                    area = tri.Getarea();
                }
                else
                {
                    WriteLine("没有这种图形");
                    area = 0;
                }
                sum += area;
                WriteLine("目前面积" + area);
                WriteLine("目前面积和" + sum);
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //show.dis();
            double area = 0;
            double sum = 0;
            Random str = new Random();
            for(int i = 0; i < 10; i++)
            {
                int n = str.Next(3);
                Shape example = ShapeFactory.Getshape(n);
                area = example.Getarea();
                sum += area;
            }
            WriteLine("面积和为：" + sum);
            ReadKey();
            
        }
    }
}
